package org.darkforest.common.services.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.darkforest.common.services.AdminService;
import org.darkforest.dao.mapper.AdminMapper;
import org.darkforest.annotation.ServiceLog;
import org.darkforest.model.Admin;
import org.darkforest.vo.MemberBase;
import org.darkforest.vo.WapAjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Libing on 2015/6/14.
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin getAdminInfoByCondition(Admin admin) {
        return adminMapper.getAdminInfoByCondition(admin);
    }

    @Override
    @ServiceLog(description ="管理员更新操作")
    public int updateByPrimaryKeySelective(Admin record) {
        return adminMapper.updateByPrimaryKeySelective(record);
    }

    @Override

    public WapAjaxMsg<MemberBase> doLogin(String userName ,String pwd) {
        WapAjaxMsg<MemberBase> msg = new WapAjaxMsg<MemberBase>();
        msg.setDatas(new MemberBase());
        Admin adminInfo = new Admin();
        adminInfo.setAdminName(userName);
        //adminInfo.setAdminPassword(DigestUtils.md5Hex(pwd));
        adminInfo=adminMapper.getAdminInfoByCondition(adminInfo);
        if (adminInfo == null) {
            msg.setError("用户不存在");
        } else if (DigestUtils.md5Hex(pwd).equals(adminInfo.getAdminPassword())) {
            //密码正确（wap端需要生成新的token），返回用户对象
            MemberBase memberBase = new MemberBase(adminInfo.getAdminId(), adminInfo.getAdminName());
            memberBase.setAdminGid(adminInfo.getAdminGid());
            memberBase.setAdminIsSuper(adminInfo.getAdminIsSuper());
            memberBase.setHealthDeptId(adminInfo.getHealthDeptId());
            msg.setDatas(memberBase);
            //更新登录时间
            adminMapper.updateLastLoginTime(adminInfo.getAdminId());
        } else {
            msg.setError("用户名密码不正确");
        }
        return msg;

    }
}
